package com.edu.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * @Author:zyn
 * @Date:2025/11/12--17:27:08
 */
@Component
@Aspect
public class MyAdvise2 {

    private Logger logger=LoggerFactory.getLogger(MyAdvise2.class);

    @Before("execution(* com.edu.service..*.add*(..)) ||" +
            "execution(* com.edu.service..*.delete*(..)) ||" +
            "execution(* com.edu.service..*.update*(..))")
    public void logBeforeAdvise(JoinPoint jt){
        logger.debug(jt.getTarget()+":"+jt.getSignature().getName()+"()方法执行开始于："+new Date());
    }

    @Around("execution(* com.edu.service..*.select*(..))")
    public Object logAroundAdvise(ProceedingJoinPoint pjt){
        Object result=null;
        try {
            Long start=System.currentTimeMillis();
            result=pjt.proceed();
            Thread.sleep(3000);
            Long end=System.currentTimeMillis();
            logger.debug(pjt.getTarget()+":"+pjt.getSignature().getName()+"()方法执行耗时："+(end-start));
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return result;
    }
    @AfterReturning(pointcut = "execution(* com.edu.service..*.select*(..))",returning = "result")
    public void afterReturnAdvise(JoinPoint pt,Object result){
        System.out.println("执行了后置返回通知");
    }

    @AfterThrowing(pointcut = "execution(* com.edu.service..*.*(..))",throwing = "e")
    public void afterThrowAdvise(JoinPoint pt,Exception e){
        System.out.println("执行了后置异常通知");
    }
    @After("execution(* com.edu.service..*.*(..))")
    public void afterAdvise(JoinPoint pt){
        System.out.println("执行了后置通知");
    }
}
